<template>
  <span class="time-to theme1" :class="className">
    <em v-for="(it, i) in list" :key="i" :class="it.className">
      {{ it.text }}
    </em>
  </span>
</template>

<script lang="ts">
// @ts-ignore
export default {
  name: "TimeTo",
  props: {
    second: {
      type: Number,
      default: 0
    },
    size: {
      type: String,
      default: ""
    }
  },
  computed: {
    list() {
      let { second } = this;
      const list = [];
      second < 0 && (second = 0);
      const timeLeftDay = this.timeLeftDay(second);
      const timeLeftArr = [];
      timeLeftArr.push(this.timeLeftH(second));
      timeLeftArr.push(this.timeLeftM(second));
      timeLeftArr.push(this.timeLeftS(second));
      if (timeLeftDay) {
        timeLeftDay.toString().split("").forEach((n: number) => {
          list.push({text: n, className: "isNum"});
        });
        // list.push({text: timeLeftDay, className: "isNum"});
        list.push({text: "天", className: ""});
      }
      timeLeftArr.forEach((num, i) => {
        const n = (num + "").padStart(2, "0");
        if (i) {
          list.push({text: ":", className: ""});
        }
        for(let c = 0; c < n.length; c++){
          list.push({text: n[c], className: "isNum"});
        }
        // list.push({text: n, className: "isNum"});
      });

      return list;
    },
    className(){
      if(this.size){
        return "time-to_"+this.size
      }
    }
  },
  methods: {
    timeLeftDay(timeLeft:number) {
      return Math.floor(timeLeft / 24 / 60 / 60);
    },
    timeLeftH(timeLeft:number) {
      return Math.floor((timeLeft / 60 / 60) % 24);
    },
    timeLeftM(timeLeft:number) {
      return Math.floor((timeLeft / 60) % 60);
    },
    timeLeftS(timeLeft:number) {
      return Math.floor(timeLeft % 60);
    }
  }
};
</script>
<style lang="scss" scoped>
.time-to.theme1 {
  --num-height: 20px;
  --num-horizontal-padding: 3px;
  --horizontal-gap: 3px;
  font-weight: bold;
  font-size: 12px;
  color: #ff2c2c;
  display: inline-flex;
  align-items: center;
  gap: var(--horizontal-gap);
  &.time-to_small{
    --num-height: 16px;
    --num-horizontal-padding: 1px;
    --horizontal-gap: 2px;
  }
  .isNum {
    height: var(--num-height);
    line-height: calc(var(--num-height) - 2px);
    padding: 0 var(--num-horizontal-padding);
    background: rgba(255, 44, 44, 0.1);
    border: 1px solid rgba(255, 44, 44, 0.2);
  }
}
</style>
